<?php
/**
 * Create By: PhpStorm
 * User: niechenguang
 * File: Circle.php
 * Date: 2017/1/2
 * Time: 23:13
 */

namespace app\api\model\page\note;

use app\api\model\dao\Note;
use app\api\model\dao\Circle;
use app\api\model\dao\NoteReply;
use app\api\model\dao\TaskLog;
use app\api\model\dao\UserCurrency;
use app\api\model\data\note\NoteData;
use app\library\Error;
use app\library\Flag;
use app\library\PageBase;
use think\Exception;
use think\Validate;
use think\Log;

class UpdateNote extends PageBase
{

    private $objNote;
    private $objCircle;
    private $objTaskLog;
    private $objNoteData;
    private $objUserCurrency;
    private $objNoteReply;

    public function __construct()
    {
        parent::__construct();

        $this->objNote = new Note();
        $this->objCircle = new Circle();
        $this->objTaskLog = new TaskLog();
        $this->objNoteData = new NoteData();
        $this->objUserCurrency = new UserCurrency();
        $this->objNoteReply = new NoteReply();
    }

    /**
     * @param $param
     * @return array|bool
     * @DESC 验证接口字段
     */
    public function checkParam($param)
    {
        $rule = [
            'note_id' => 'require',
            'type' => 'require',
        ];
        $msg = [
            'note_id.require' => '请选择帖子id',
            'type.require' => '请选择更新的类型',
        ];

        $validate = new Validate($rule, $msg);
        $result = $validate->check($param);
        if (!$result) {
            return $validate->getError();
        }
        return '';
    }

    public function excute($param)
    {
//        $ret = $this->checkToken($param);
//        if (false === $ret) {
//            return $this->getRes(Error::ERR_NO_AUTH);
//        }

        // 1、参数校验
        $ret = $this->checkParam($param);
        if ($ret) {
            return $this->getRes(Error::ERR_PARAM, $ret);
        }

        $note_info = $this->objNote->getNoteByid($param['note_id']);
        if (empty($note_info)) {
            return $this->getRes(Error::ERR_NOTE_NOT_EXIST);
        }
        $types = explode(',', $param['type']);
        if (in_array('delete', $types)) {
            $ret = $this->delNote($param);
            if (false === $ret) {
                return $this->getRes(Error::ERR_SYS);
            }
            return $this->getRes();
        } else {
            $param['type'] = $types;
            $this->updateNoteFlag($param);
            if (false === $ret) {
                return $this->getRes(Error::ERR_SYS);
            }
            return $this->getRes();
        }

        /*

        $types = explode(',',$param['type']);
        if(in_array('delete',$types)){
            $ret = $this->objNote->delNote($param['note_id']);
        }else{
            $flag = 0;
            foreach ($types as $value){
                $flag = $flag | $value;
                $ret = $this->objNote->updateFlag(['note_id' => $param['note_id'], 'flag' => $flag]);
            }
        }
        if(false === $ret){
            return $this->getRes(Error::ERR_SYS);
        }
        return $this->getRes();
*/
        /*
        if (!empty($param['is_delete']) && 1 == $param['is_delete']) {
            $ret = $this->objNote->delNote($param['note_id']);
        } else {
            if (!empty($param['type'])) {
                $ret = $this->objNote->updateFlag(['note_id' => $param['note_id'], 'flag' => $note_info['flag'] | $param['type']]);
                if (($param['type'] & Flag::FLAG_NOTE_ORIGIN) == Flag::FLAG_NOTE_ORIGIN) {
                    $this->objNote->updateNote(['note_id' => $param['note_id'], 'original' => 1]);
                }
            }
        }

        if (false === $ret) {
            return $this->getRes(Error::ERR_SYS);
        }
        return $this->getRes();
        */
    }

    public function delNote($param)
    {
        return $this->objNote->delNote($param['note_id']);
    }

    public function updateNoteFlag($param)
    {
        $flag = 0;
        foreach ($param['type'] as $value) {
            $flag += $value;
        }
        return $this->objNote->updateFlag(['note_id' => $param['note_id'], 'flag' => $flag]);
    }

}
